﻿//*** Guidance generated code ***//
using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Linq;
using System.Web;

using ProjectBase.Core;
using ProjectBase.Core.Dao;
using ProjectBase.Core.Service;

namespace $safeprojectname$.Web
{
    /// <summary>
    ///     HttpModule for Data layer support
    /// </summary>
    public class DataHttpModule : IHttpModule
    {
        #region IHttpModule Members
        /// <summary>
        ///     Disposes of the resources (other than memory) used by the module that implements <see cref = "T:System.Web.IHttpModule" />.
        /// </summary>
        public void Dispose() { }

        /// <summary>
        ///     Initializes a module and prepares it to handle requests.
        /// </summary>
        /// <param name = "context">An <see cref = "T:System.Web.HttpApplication" /> that provides access to the methods, properties, and events common to all application objects within an ASP.NET application</param>
        public void Init(HttpApplication context)
        {
            var sessionStorage = new WebSessionStorage();
            NHSession.Initialize(sessionStorage);
            DbSession.Initialize(sessionStorage);
            XmlSession.Initialize(sessionStorage);
            context.EndRequest += CloseSessionHandler;
            context.Error += CloseSessionHandler;
        }
        #endregion

        /// <summary>
        ///     Closes the session handler.
        /// </summary>
        /// <param name = "sender">The sender.</param>
        /// <param name = "e">The <see cref = "System.EventArgs" /> instance containing the event data.</param>
        protected virtual void CloseSessionHandler(object sender, EventArgs e)
        {
            try
            {
                NHSession.Dispose();
            }
            catch (Exception ex)
            {
                PublishingManager.Publish(this, "fatal error on disposes NHibernate", ex);
            }
            try
            {
                DbSession.Dispose();
            }
            catch (Exception ex)
            {
                PublishingManager.Publish(this, "fatal error on disposes DbSession ", ex);
            }
            try
            {
                XmlSession.Dispose();
            }
            catch (Exception ex)
            {
                PublishingManager.Publish(this, "fatal error on disposes XmlSession ", ex);
            }
        }

        #region session providers
        INHSessionHandler _nhSession;
        protected virtual INHSessionHandler NHSession
        {
            get { return _nhSession ?? (_nhSession = Factory.CreateInstance<INHSessionHandler>()); }
        }
        IDbSessionHandler _dbSession;
        protected virtual IDbSessionHandler DbSession
        {
            get { return _dbSession ?? (_dbSession = Factory.CreateInstance<IDbSessionHandler>()); }
        }
        IXmlSessionHandler _xmlSession;
        protected virtual IXmlSessionHandler XmlSession
        {
            get { return _xmlSession ?? (_xmlSession = Factory.CreateInstance<IXmlSessionHandler>()); }
        }
        #endregion session providers
    }
}